package com.sample.navigation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.sample.navigation.adapter.NativeFragmentPagerAdapter;
import com.sample.navigation.fragment.Fragment1;
import com.sample.navigation.fragment.Fragment2;
import com.sample.navigation.fragment.Fragment3;
import com.sample.navigation.fragment.Fragment4;
import com.sample.navigation.util.ScreenMetricUtil;
import com.sample.navigation.util.WindowStyleUtil;
import com.sample.navigation.view.CircleTextView;
import com.sample.navigation.view.IndicatorView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private final String TAG = MainActivity.class.getSimpleName();
    private ViewPager viewPager;
    private int currentItem = 0;
    private float posX = 0;
    private TabItem[] tabItems = new TabItem[4];
    private IndicatorView indicatorView;

    private class TabItem{
        CircleTextView tabIndex;
        TextView tabText;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            WindowStyleUtil.setStatusColor(getWindow(), getResources().getColor(R.color.bg_active));
            WindowStyleUtil.setNavigationColor(getWindow(), Color.TRANSPARENT);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initComponents();
    }

    private void initComponents(){
        //获得屏幕尺寸信息
        ScreenMetricUtil.initialize(this);
        //设置返回按钮响应
        findViewById(R.id.btn_back)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
        //设置tab项
        for (int i=0; i!=tabItems.length;i++){
            tabItems[i] = new TabItem();
        }
        tabItems[0].tabIndex = (CircleTextView)findViewById(R.id.tab_index_1);
        tabItems[0].tabText = findViewById(R.id.tab_text_1);
        tabItems[1].tabIndex = (CircleTextView)findViewById(R.id.tab_index_2);
        tabItems[1].tabText = findViewById(R.id.tab_text_2);
        tabItems[2].tabIndex = (CircleTextView)findViewById(R.id.tab_index_3);
        tabItems[2].tabText = findViewById(R.id.tab_text_3);
        tabItems[3].tabIndex = (CircleTextView)findViewById(R.id.tab_index_4);
        tabItems[3].tabText = findViewById(R.id.tab_text_4);
        setHighLight(0);
        //设置tab指示器
        indicatorView = (IndicatorView)findViewById(R.id.indicator);
        indicatorView.setSplitIndex(4);
        indicatorView.setSelect(0);
        //设置内容
        viewPager = (ViewPager)findViewById(R.id.content);
        ArrayList<Fragment> pageList = new ArrayList<>();
        pageList.add(new Fragment1());
        pageList.add(new Fragment2());
        pageList.add(new Fragment3());
        pageList.add(new Fragment4());
        viewPager.setAdapter(new NativeFragmentPagerAdapter(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, pageList));
        viewPager.setOffscreenPageLimit(pageList.size());
        //设置切换到下一页时文字背景高光和指示器位置
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d(TAG, "position: " + position);
            }
            @Override
            public void onPageSelected(int position) {
                setHighLight(position);
                setSelected(position);
                if(position < currentItem){
                    cancelHighLight(currentItem);
                }
                currentItem = position;
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //设置viewpager只能向左滑动,即返回上一步
        //如果返回上一步也不需要将false改为true即可
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        posX = event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(event.getX() - posX < 0){
                            return true;
                        }
                        posX = event.getX();
                        break;
                }
                return false;
            }
        });
    }

    public void switchToNext(int index){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(index);
            }
        });
    }

    //设置tab高光
    private void setHighLight(int index){
        tabItems[index].tabIndex.setHighLight(true);
        tabItems[index].tabText.setTextColor(getResources().getColor(R.color.text_color_light));
    }

    //取消tab高光
    private void cancelHighLight(int index){
        tabItems[index].tabIndex.setHighLight(false);
        tabItems[index].tabText.setTextColor(getResources().getColor(R.color.text_color_inactive));
    }

    private void setSelected(int index){
        indicatorView.setSelect(index);
    }
}